package com.anjoyo.xgh.musicshare.activity;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.adapter.DownSongAdapter;
import com.anjoyo.xgh.musicshare.api.CollectApi;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.Song;

public class DownAct extends Activity {
	Intent intent;
	ArrayList<Song> songs;
	ListView listView;
	int currentpos = -1;
	DownSongAdapter adapter;
	Context con;
	MusicDBhelper dBhelper;
	Button btndelet;
	TextView tvdelet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.down_layout);
		con = this;
		intent = getIntent();
		songs = (ArrayList<Song>) intent.getSerializableExtra("songs");
		dBhelper = new MusicDBhelper(con);

		dolistview();
		
		findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btndelet = (Button) findViewById(R.id.btn_delet);
		btndelet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				adapter.deletsongs();
				adapter.unshowcheckbox();
				tvdelet.setVisibility(View.VISIBLE);
				btndelet.setVisibility(View.GONE);
				System.out.println("点击事件");
			}
		});
		tvdelet = (TextView) findViewById(R.id.tv_delet);
		tvdelet.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				adapter.showcheckbox();
				tvdelet.setVisibility(View.GONE);
				btndelet.setVisibility(View.VISIBLE);
				System.out.println("长按事件");
				return false;
			}
		});
	}
	
	private void dolistview() {
		listView = (ListView) findViewById(R.id.lv_downsong);
		adapter = new DownSongAdapter(this, songs);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				adapter.changeColor(arg2);
			}
		});
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				adapter.changeColor(arg2);
				currentpos = arg2;
				new AlertDialog.Builder(con)
				.setSingleChoiceItems(new String[] { "收藏","分享","删除" }, 0,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (which == 0) {
									dialog.dismiss();
									Song song = (Song) adapter
											.getItem(currentpos);
									CollectApi api = new CollectApi();
									api.clodesong(con, song);
								} else if(which==1) {
									dialog.dismiss();
									Song song = (Song) adapter
											.getItem(currentpos);
									Intent intent = new Intent(con,
											ShareAct.class);
									intent.putExtra("song", song);
									startActivity(intent);
								} else if(which==2){
									dialog.dismiss();
									
									Song song = adapter.getItem(currentpos);
									adapter.deletsong(currentpos);
									File file = new File(song.getLocal_url());
									file.delete();
									
									int i = dBhelper.deletsong(song.getMusicid());
									if (i!=0) {
										Toast.makeText(con, song.getName()+"|删除成功", 0).show();
									}else {
										Toast.makeText(con,song.getName()+"|删除失败", 0).show();
									}
								}
							}
						}).setNegativeButton("关闭", null).create()
				.show();
				return false;
			}
		});
	}
}
